home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Win32 / Test.pl < prev    next >
Encoding:
Text File  |  1999-12-28  |  12.7 KB  |  365 lines

  1.  
  2.     use Win32::ODBC;
  3.  
  4.  
  5.     $TempDSN = "Win32 ODBC Test --123xxYYzz987--";
  6.     $iTempDSN = 1;
  7.  
  8.     if (!($DSN = $ARGV[0])){
  9.         $DSN = $TempDSN;
  10.     }
  11.     $MaxRows = 8 unless defined ($MaxRows = $ARGV[1]);
  12.  
  13.     $DriverType = "Microsoft Access Driver (*.mdb)";
  14.     $Desc = "Description=The Win32::ODBC Test DSN for Perl";
  15.     $Dir = `cd`;
  16.     chop $Dir;
  17.     $DBase = "ODBCTest.mdb";
  18.  
  19.     $iWidth=60;
  20.     %SQLStmtTypes = (SQL_CLOSE, "SQL_CLOSE", SQL_DROP, "SQL_DROP", SQL_UNBIND, "SQL_UNBIND", SQL_RESET_PARAMS, "SQL_RESET_PARAMS");
  21.  
  22.  
  23.     ($Name, $Version, $Date, $Author, $CompileDate, $CompileTime, $Credits) = Win32::ODBC::Info();
  24.     print "\n";
  25.     print "\t+", "=" x ($iWidth), "+\n";
  26.     print "\t|", Center("", $iWidth), "|\n";
  27.     print "\t|", Center("", $iWidth), "|\n";
  28.     print "\t|", Center("$Name", $iWidth), "|\n";
  29.     print "\t|", Center("-" x length("$Name"), $iWidth), "|\n";
  30.     print "\t|", Center("", $iWidth), "|\n";
  31.  
  32.     print "\t|", Center("Version $Version ($Date)", $iWidth), "|\n";
  33.     print "\t|", Center("by $Author", $iWidth), "|\n";
  34.     print "\t|", Center("Compiled on $CompileDate at $CompileTime.", $iWidth), "|\n";
  35.     print "\t|", Center("", $iWidth), "|\n";
  36.     print "\t|", Center("Credits:", $iWidth), "|\n";
  37.     print "\t|", Center(("-" x length("Credits:")), $iWidth), "|\n";
  38.     foreach $Temp (split("\n", $Credits)){
  39.         print "\t|", Center("$Temp", $iWidth), "|\n";
  40.     }
  41.     print "\t|", Center("", $iWidth), "|\n";
  42.     print "\t+", "=" x ($iWidth), "+\n";
  43.  
  44.     PrintTest(1, "Dump available ODBC Drivers");
  45.     print "\nAvailable ODBC Drivers:\n";
  46.     if (!(%Drivers = Win32::ODBC::Drivers())){
  47.         $Failed{'Test 1'} = "Drivers(): " . Win32::ODBC::Error();
  48.     }
  49.     foreach $Driver (keys(%Drivers)){
  50.         print "  Driver=\"$Driver\"\n  Attributes: ", join("\n" . " "x14, sort(split(';', $Drivers{$Driver}))), "\n\n";
  51.     }
  52.  
  53.  
  54.     PrintTest(2,"Dump available datasources");
  55.  
  56.     print "\nHere are the available datasources...\n";
  57.     if (!(%DSNs = Win32::ODBC::DataSources())){
  58.         $Failed{'Test 2'} = "DataSources(): " . Win32::ODBC::Error();
  59.     }
  60.     foreach $Temp (keys(%DSNs)){
  61.         if (($Temp eq $TempDSN) && ($DSNs{$Temp} eq $DriverType)){
  62.             $iTempDSNExists++;
  63.         }
  64.         if ($DSN =~ /$Temp/i){
  65.             $iTempDSN = 0;
  66.             $DriverType = $DSNs{$Temp};
  67.         }
  68.         print "\tDSN=\"$Temp\" (\"$DSNs{$Temp}\")\n";
  69.     }
  70.  
  71.     if ($iTempDSN){
  72.         PrintTest("2.5", "Create a Temporary DSN");
  73.  
  74.         print "\n\tCould not find the DSN (\"$DSN\") so we will\n\tuse a temporary one (\"$TempDSN\")...\n\n";
  75.  
  76.         $DSN = $TempDSN;
  77.  
  78.         if (! $iTempDSNExists){
  79.             print "\tAdding DSN \"$DSN\"...";
  80.             if (Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType, ("DSN=$DSN", "Description=The Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD="))){
  81.                 print "Successful!\n";
  82.             }else{
  83.                 print "Failure\n";
  84.                 $Failed{'Test 2.5'} = "ConfigDSN(): Could not add \"$DSN\": " . Win32::ODBC::Error();
  85.                 $DriverType = $DSNs{$Temp};
  86.                 $DSN = $Temp;
  87.                 print "\n\tCould not add a temporary DSN so using the last one listed:\n";
  88.                 print "\t\t$DSN ($DriverType)\n";
  89.  
  90.             }
  91.         }
  92.     }
  93.  
  94.  
  95.     print "\n\nWe are using the DSN:\n\tDSN = \"$DSN\"\n";
  96.     print "\tDriver = \"$DriverType\"\n\n";
  97.  
  98.  
  99.     PrintTest(3, "Open several ODBC connections");
  100.     print "\n\tOpening ODBC connection for \"$DSN\"...\n\t\t";
  101.     if (!($O = new Win32::ODBC($DSN))){
  102.         print "Failure. \n\n";
  103.         $Failed{'Test 3a'} = "new(): " . Win32::ODBC::Error();
  104.         PresentErrors();
  105.         exit();
  106.     }else{
  107.         print "Success (connection #", $O->Connection(), ")\n\n";
  108.     }
  109.  
  110.     print "\tOpening ODBC connection for \"$DSN\"...\n\t\t";
  111.     if (!($O2 = new Win32::ODBC($DSN))){
  112.         $Failed{'Test 3b'} = "new(): " . Win32::ODBC::Error();
  113.         print "Failure. \n\n";
  114.     }else{
  115.         print "Success (connection #", $O2->Connection(), ")\n\n";
  116.     }
  117.  
  118.     print "\tOpening ODBC connection for \"$DSN\"\n\t\t";
  119.     if (!($O3 = new Win32::ODBC($DSN))){
  120.         $Failed{'Test 3c'} = "new(): " . Win32::ODBC::Error();
  121.         print "Failure. \n\n";
  122.     }else{
  123.         print "Success (connection #", $O3->Connection(), ")\n\n";
  124.     }
  125.  
  126.  
  127.     PrintTest(4, "Close all but one connection");
  128.  
  129.     print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O2->GetConnections())), "\"\n";
  130.     print "\tClosing ODBC connection #", $O2->Connection(), "...\n";
  131.     print "\t\t...", (($O2->Close())? "Successful.":"Failure."), "\n";
  132.  
  133.     print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O3->GetConnections())), "\"\n";
  134.     print "\tClosing ODBC connection #", $O3->Connection(), "...\n";
  135.     print "\t\t...", (($O3->Close())? "Successful.":"Failure."), "\n";
  136.  
  137.     print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O2->GetConnections())), "\"\n";
  138.  
  139.     PrintTest(5, "Set/query Max Buffer size for a connection");
  140.  
  141.     srand(time);
  142.     $Temp = int(rand(10240)) + 10240;
  143.     print "\nMaximum Buffer Size for connection #", $O->Connection(), ":\n";
  144.     print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  145.  
  146.     print "\tSetting Maximum Buffer Size to $Temp...  it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  147.     print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  148.  
  149.     $Temp += int(rand(10240)) + 102400;
  150.     print "\tSetting Maximum Buffer Size to $Temp... (can not be more than 102400)\n\t\t...it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  151.     print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  152.  
  153.     $Temp = int(rand(1024)) + 2048;
  154.     print "\tSetting Maximum Buffer Size to $Temp...  it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  155.  
  156.     print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  157.  
  158.  
  159.     PrintTest(6, "Set/query Stmt Close Type");
  160.  
  161.     print "\n\tStatement Close Type is currently set as ", $O->GetStmtCloseType(), " " . $O->Error . "\n";
  162.     print "\tSetting Statement Close Type to SQL_CLOSE: (returned code of ",  $O->SetStmtCloseType(SQL_CLOSE), ")" . $O->Error . "\n";
  163.     print "\tStatement Close Type is currently set as ", $O->GetStmtCloseType(), " " . $O->Error ."\n";
  164.  
  165.  
  166.     PrintTest(7, "Dump DSN for current connection");
  167.  
  168.     if (! (%DSNAttributes = $O->GetDSN())){
  169.         $Failed{'Test 7'} = "GetDSN(): " . $O->Error();
  170.     }else{
  171.         print"\nThe DSN for connection #", $O->Connection(), ":\n";
  172.         print "\tDSN...\n";
  173.         foreach (sort(keys(%DSNAttributes))){
  174.             print "\t$_ = \"$DSNAttributes{$_}\"\n";
  175.         }
  176.     }
  177.  
  178.  
  179.  
  180.     PrintTest(8, "Dump list of ALL tables in datasource");
  181.  
  182.     print "\nList of tables for \"$DSN\"\n\n";
  183.     $Num = 0;
  184.     if ($O->Catalog("", "", "%", "'TABLE','VIEW','SYSTEM TABLE', 'GLOBAL TEMPORARY','LOCAL TEMPORARY','ALIAS','SYNONYM'")){
  185.  
  186.         print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n";
  187.         print "\tRenaming cursor to \"TestCursor\"...", (($O->SetCursorName("TestCursor"))? "Success":"Failure"), ".\n";
  188.         print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n\n";
  189.  
  190.         @FieldNames = $O->FieldNames();
  191.  
  192.         $~ = "Test_8_Header";
  193.         write;
  194.  
  195.         $~ = "Test_8_Body";
  196.         while($O->FetchRow()){
  197.             undef %Data;
  198.             %Data = $O->DataHash();
  199.             write;
  200.         }
  201.     }
  202.     print "\n\tTotal number of tables displayed: $Num\n";
  203.  
  204.  
  205.  
  206.     PrintTest(9, "Dump list of non-system tables and views in datasource");
  207.  
  208.     print "\n";
  209.     $Num = 0;
  210.  
  211.     foreach  $Temp ($O->TableList("", "", "%", "TABLE, VIEW, SYSTEM_TABLE")){
  212.         $Table = $Temp;
  213.         print "\t", ++$Num, ".) \"$Temp\"\n";
  214.     }
  215.     print "\n\tTotal number of tables displayed: $Num\n";
  216.  
  217.  
  218.     PrintTest(10, "Dump contents of the table: \"$Table\"");
  219.  
  220.     print "\n";
  221.  
  222.     print "\tResetting (dropping) cursor...", (($O->DropCursor())? "Successful":"Failure"), ".\n\n";
  223.  
  224.     print "\tCurrently the cursor type is: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n";
  225.     print "\tSetting Cursor to Dynamic (", ($O->SQL_CURSOR_DYNAMIC), ")...", (($O->SetStmtOption($O->SQL_CURSOR_TYPE, $O->SQL_CURSOR_DYNAMIC))? "Success":"Failure"), ".\n";
  226.     print "\t\tThis may have failed depending on your ODBC Driver.\n";
  227.     print "\t\tThis is not really a problem, it will default to another value.\n";
  228.     print "\tUsing the cursor type of: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n\n";
  229.  
  230.     print "\tSetting the connection to only grab $MaxRows row", ($MaxRows == 1)? "":"s", " maximum...";
  231.     if ($O->SetStmtOption($O->SQL_MAX_ROWS, $MaxRows)){
  232.         print "Success!\n";
  233.     }else{
  234.         $Failed{'Test 10a'} = "SetStmtOption(): " . Win32::ODBC::Error();
  235.         print "Failure.\n";
  236.     }
  237.  
  238.     $iTemp = $O->GetStmtOption($O->SQL_MAX_ROWS);
  239.     print "\tUsing the maximum rows: ", (($iTemp)? $iTemp:"No maximum limit"), "\n\n";
  240.  
  241.     print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n";
  242.     print "\tRenaming cursor to \"TestCursor\"...", (($O->SetCursorName("TestCursor"))? "Success":"Failure"), ".\n";
  243.     print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n\n";
  244.  
  245.     if (! $O->Sql("SELECT * FROM [$Table]")){
  246.         @FieldNames = $O->FieldNames();
  247.         $Cols = $#FieldNames + 1;
  248.         $Cols = 8 if ($Cols > 8);
  249.  
  250.         $FmH = "format Test_10_Header =\n";
  251.         $FmH2 = "";
  252.         $FmH3 = "";
  253.         $FmB = "format Test_10_Body = \n";
  254.         $FmB2 = "";
  255.  
  256.         for ($iTemp = 0; $iTemp < $Cols; $iTemp++){
  257.             $FmH .= "@" . "<" x (80/$Cols - 2) . " ";
  258.             $FmH2 .= "\$FieldNames[$iTemp],";
  259.             $FmH3 .= "-" x (80/$Cols - 1) . " ";
  260.  
  261.             $FmB .= "@" . "<" x (80/$Cols - 2) . " ";
  262.             $FmB2 .= "\$Data{\$FieldNames[$iTemp]},";
  263.         }
  264.         chop $FmH2;
  265.         chop $FmB2;
  266.  
  267.         eval"$FmH\n$FmH2\n$FmH3\n.\n";
  268.         eval "$FmB\n$FmB2\n.\n";
  269.  
  270.         $~ = "Test_10_Header";
  271.         write();
  272.         $~ = "Test_10_Body";
  273.  
  274.         while($O->FetchRow()){
  275.             undef %Data;
  276.             %Data = $O->DataHash();
  277.             write();
  278.         }
  279.  
  280.         print "\n\tNo more records available.\n";
  281.     }else{
  282.         $Failed{'Test 10'} = "Sql(): " . $O->Error();
  283.     }
  284.  
  285.     $O->Close();
  286.  
  287.     if ($iTempDSN){
  288.         PrintTest(11, "Remove the temporary DSN");
  289.         print "\n\tRemoving the temporary DSN:\n";
  290.         print "\t\tDSN = \"$DSN\"\n\t\tDriver = \"$DriverType\"\n";
  291.  
  292.         if (Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN")){
  293.             print "\tSuccessful!\n";
  294.         }else{
  295.             print "\tFailed.\n";
  296.             $Failed{'Test 11'} = "ConfigDSN(): Could not remove \"$DSN\":" . Win32::ODBC::Error();
  297.         }
  298.     }
  299.  
  300.  
  301.     PrintTest("E N D   O F   T E S T");
  302.     PresentErrors();
  303.  
  304.  
  305.  
  306.  
  307. sub Error{
  308.     my($Data) = @_;
  309.     $Data->DumpError() if ref($Data);
  310.     Win32::ODBC::DumpError() if ! ref($Data);
  311. }
  312.  
  313.  
  314. sub Center{
  315.     local($Temp, $Width) = @_;
  316.     local($Len) = ($Width - length($Temp)) / 2;
  317.     return " " x int($Len), $Temp, " " x (int($Len) + (($Len != int($Len))? 1:0));
  318. }
  319.  
  320. sub PrintTest{
  321.     my($Num, $String) = @_;
  322.     my($Temp);
  323.     if (length($String)){
  324.         $Temp = "  T E S T  $Num $String ";
  325.     }else{
  326.         $Temp = "  $Num  ";
  327.     }
  328.     $Len = length($Temp);
  329.     print "\n", "-" x ((79 - $Len)/2), $Temp, "-" x ((79 - $Len)/2 - 1), "\n";
  330.     print "\t$String\n";
  331. }
  332.  
  333. sub PresentErrors{
  334.     PrintTest("", "Error Report:");
  335.     if (keys(%Failed)){
  336.         print "The following were errors:\n";
  337.         foreach (sort(keys(%Failed))){
  338.             print "$_ = $Failed{$_}\n";
  339.         }
  340.     }else{
  341.         print "\n\nThere were no errors reported during this test.\n\n";
  342.     }
  343. }
  344.  
  345.  
  346. sub Initialize{
  347. format Test_8_Header =
  348.        @<<<<<<<<<<<<<<<<<<<<<<<<<<< @|||||||||||| @|||||||||||| @|||||||||||
  349.        $FieldNames[0],     $FieldNames[1], $FieldNames[2], $FieldNames[3]
  350.        ---------------------------- ------------- ------------- ------------
  351. .
  352. format Test_8_Body =
  353.    @>. @<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<
  354.  ++$Num, $Data{$FieldNames[0]},  $Data{$FieldNames[1]},   $Data{$FieldNames[2]}, $Data{$FieldNames[3]}
  355. .
  356. format Test_9_Header =
  357.           @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
  358.            $FieldNames[0],  $FieldNames[1],   $FieldNames[2], $FieldNames[3]
  359. .
  360. format Test_9_Body =
  361.           @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
  362.            $Data{$FieldNames[0]},  $Data{$FieldNames[1]},   $Data{$FieldNames[2]}, $Data{$FieldNames[3]}
  363. .
  364. }
  365.